package com.sz.app.oss.api.trade.bill;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.trade.bill.service.TrdBillingTaskService;
import com.sz.biz.trade.bill.dto.TrdBillingDetailDto;
import com.sz.common.base.dto.CommonModel;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.StringUtils;
import com.sz.biz.logistics.bill.entity.BillingTaskStatus;
import com.sz.biz.trade.bill.dto.TrdBillingTaskQueryDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Function: 贸易账单任务管理 <br>
 * Author: changwentao <br>
 * Date: 2017-07-18 14:19:00
 */
@RestController
@RequestMapping("${api}/biz/financial/trd/task/pay")
@Api(description = " ", tags = "058-003、应付账单任务管理")
public class TrdBillingTaskPayController extends BaseController {

    @Autowired
    private TrdBillingTaskService billingTaskService;


    @ApiOperation(value = "生成账单", notes = "生成账单" + Permissions.BIZ_MGT_TRD_BILL_PAY)
    @ApiImplicitParams({@ApiImplicitParam(name = "trdBillingTaskQueryDto", value = "生成账单参数信息\r\n "
            + "bizCompanyIds(业务方数组),platformIds(平台方数组), startEffectTime(yyyy-MM-dd HH:mm:ss), endEffectTime(yyyy-MM-dd HH:mm:ss)",
            required = true, dataType = "TrdBillingTaskQueryDto", paramType = "body")})
    @RequestMapping(value = "/generate", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_TRD_BILL_PAY)
    public ResultDto generateBill(@RequestBody TrdBillingTaskQueryDto trdBillingTaskQueryDto) {
        String taskCode = billingTaskService.generateBill(2,trdBillingTaskQueryDto);
        ResultDto result = this.getTaskStartSuccessDto(taskCode);
        return result;
    }

    @ApiOperation(value = "根据任务编码删除任务-只能删除已完成的任务", notes = "根据任务编码删除任务-只能删除已完成的任务<br>权限：" + Permissions.BIZ_MGT_TRD_BILL_PAY)
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "taskCodes", value = "taskCodes", required = true, dataType = "List", paramType = "body")})
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_TRD_BILL_PAY)
    public ResultDto batchDeleteBillingByNos(@RequestBody List<String> taskCodes) {
        billingTaskService.batchdeleteByTaskCode(taskCodes);
        return getDeleteSuccessDto(MessagesCodes.MODULE_FINANCIAL_TASK);
    }

    @ApiOperation(value = "重新生成账单", notes = "重新生成账单<br>权限：" + Permissions.BIZ_MGT_TRD_BILL_PAY)
    @ApiImplicitParams({@ApiImplicitParam(name = "taskCodes", value = "重试的任务编码列表", required = true, dataType = "List", paramType = "body")})
    @RequestMapping(value = "/regenerate", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_TRD_BILL_PAY)
    public ResultDto regenerateBill(@RequestBody List<String> taskCodes) {
        billingTaskService.regenerate(taskCodes);
        ResultDto result = this.getOperationSuccessDto(MessagesCodes.MODULE_FINANCIAL);
        return result;
    }

    @RequestMapping(value = "/status", method = RequestMethod.GET)
    @ApiOperation(value = "所有账单任务状态数据", notes = "所有账单任务状态数据")
    public ResultDto getAllBillingTaskStatus() {
        List<CommonModel> list = billingTaskService.getAllTaskStatus();
        ResultDto result = new ResultDto();
        result.setData(list);
        return result;
    }

    @ApiOperation(value = "账单任务列表", notes = "账单任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskCode", value = "任务编码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "taskStatus", value = "任务状态", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startEffectTime", value = "起始账单周期(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endEffectTime", value = "结束账单周期(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "taskStartTime", value = "起始任务周期(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "taskEndTime", value = "结束任务周期(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public ResultDto findBillingTaskList(
            @RequestParam(required = false) String taskCode,
            @RequestParam(required = false) BillingTaskStatus taskStatus,
            @RequestParam(required = false) String startEffectTime,
            @RequestParam(required = false) String endEffectTime,
            @RequestParam(required = false) String taskStartTime,
            @RequestParam(required = false) String taskEndTime,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            @RequestParam(required = false, defaultValue = "10") int pageSize,
            @RequestParam(required = false) String sort,
            @RequestParam(required = false, defaultValue = "true") Boolean asc) {

        QueryParams params = new QueryParams(pageSize, pageIndex, "", sort, asc);
        params.put("startEffectTime", StringUtils.isEmpty(startEffectTime)?null:startEffectTime);
        params.put("endEffectTime", StringUtils.isEmpty(endEffectTime)?null:endEffectTime);
        params.put("taskStartTime", StringUtils.isEmpty(taskStartTime)?null:taskStartTime);
        params.put("taskEndTime", StringUtils.isEmpty(taskEndTime)?null:taskEndTime);

        params.put("taskCode", taskCode);
        params.put("taskStatus", taskStatus);
        params.put("type", 2);
        ResultDto dto = billingTaskService.findBillingTaskList(params);
        return dto;
    }


    @ApiOperation(value = "账单任务详情", notes = "账单任务详情<br>权限：" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskCode", value = "任务编码", required = true, dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/detail", method = {RequestMethod.GET})
    public ResultDto billingTaskDetail(@RequestParam(required = false) String taskCode) {
        TrdBillingDetailDto billingDetailDto = billingTaskService.billingTaskDetail(taskCode);
        ResultDto dto = new ResultDto();
        dto.setData(billingDetailDto);
        return dto;
    }
}
